import { State } from "framework/FSM/AState";
import { BuildTask } from "./buildTask";
import { UpgradeTask } from "./UpgradeTask";
import { HarvestTask } from "./HarvestTask";
import { PorterTask } from "./PorterTask";
import { BehaviorMask } from "GameLoop/WorldState/WorldConfig";
import { FillTask } from "./FillTask";
import { RepairTask } from "./RepairTask";
import { ScoutTask } from "./ScoutTask";
import { ClaimTask } from "./ClaimTask";
import { TransportTask } from "./TransportTask";
import { AttackTask } from "./AttackTask";
import { AccurateTransportTask } from "./AccurateTransportTask";
import { DefendTask } from "./DefendTask";
import { DefendRamTask } from "./DefendRamTask";
import { HelpBuildTask } from "./HelpBuildTask";
import { HarvestMinerial } from "./HarvestMinerial";
import { PorterMinerialTask } from "./PorterMinerialTask";
import { HelpUpgradeTask } from "./HelpUpgradeTask";

import { HarvestDepo } from "./HarvestDepo";
import { PorterDepoTask } from "./PorterDepoTask";
import { PorterPowerTask } from "./PorterPowerTask";
import { cpu_used } from "utils/decorate/decorate";

/**
 * 行为模式
 * 采集行为
 * 升级行为
 */ 
/*@profile*/
export class TaskMode<T extends IStateArg<Creep>>  extends State<T>
{
    //  不同的任务对应有哪些Action
    private TaskMap:Record<CreepTaskType|TeamTaskType,[BehaviorMask, State<T>][]> = {
        build:[[BehaviorMask.build, new BuildTask<T>()]],
        upgrade:[[BehaviorMask.upgrade, new UpgradeTask<T>()]],
        harvest:[
            [BehaviorMask.harvest, new HarvestTask<T>()],
            [BehaviorMask.porter, new PorterTask<T>()],
        ],
        harvestM:[
            [BehaviorMask.harvest, new HarvestMinerial<T>()],
            [BehaviorMask.porter, new PorterMinerialTask<T>()],
        ],
        depo:[
            [BehaviorMask.harvest, new HarvestDepo<T>()],
            [BehaviorMask.porter, new PorterDepoTask<T>()],
        ],
        fill:[
            [BehaviorMask.movefill, new FillTask<T>()],
        ],
        repair:[
            [BehaviorMask.repair, new RepairTask<T>()],
        ],
        scout:[
            [BehaviorMask.scout, new ScoutTask<T>()],
        ],
        claim:[
            [BehaviorMask.claim, new ClaimTask<T>()]
        ],
        transport:[
            [BehaviorMask.porter, new TransportTask<T>()],
        ],
        transportAccurate:[
            [BehaviorMask.porter, new AccurateTransportTask<T>()],
            
        ],
        defend:[ 
            [BehaviorMask.attack,new AttackTask<T>()],
            [BehaviorMask.defend,new DefendTask<T>()],
            [BehaviorMask.porter, new PorterPowerTask<T>()],
        ],
        defendram:[ 
            [BehaviorMask.attack,new AttackTask<T>()],
            [BehaviorMask.defend,new DefendRamTask<T>()]
        ],
        helpBuild:[
            [BehaviorMask.build,new HelpBuildTask<T>()],
            [BehaviorMask.upgrade, new HelpUpgradeTask<T>()]
        ]
        
    }  ;
    //
    @cpu_used((arg:T)=>{
        return `${arg.entity.GUID()}-${arg.entity.memory.taskid}-getActions`
    },
    (arg:any,cpu:number)=>
    {
        return  cpu>1
    }
    )
    public  getActions(arg:T):Action[]
    {
        const memory = arg.entity.getNodeMemory();
        if(!memory.taskid)
        {
            return [];
        }
        //if(!memory.tasktype)
        //{
            const task = arg.entity.ownerRoom().getTaskById<ITask<ICreepTaskData>>(memory.taskid,["creep","team"]);
            
            if(!task &&   !arg.entity.getEntityByID(memory.taskid))
            {
                arg.entity.log("warn",` 任务不存在删除任务:${arg.entity.memory.taskid}`);
                // 任务不存在
                delete memory.taskid;
                arg.entity.ownerRoom().addCreep(arg.entity.name,arg.entity.memory.Role);
                return [];
            }
            // if(!memory.tasktype)
          const tasktype = task?.data.type;
       // }
        if(! tasktype || ! this.TaskMap[tasktype])
        {
            if(!arg.entity.getEntityByID(memory.taskid))
            {
                arg.entity.log("warn",`未知的任务类型:${tasktype}`);
            }
         
            return []
        }
        const behaviors = this.TaskMap[tasktype];
        const msk =  memory.msk;
        for(const k of behaviors)
        {
            const mask = k[0];
            if(msk & mask)
            {
                arg.task = task;
                const actions =  k[1].getActions(arg);
                if(actions.length>0)
                {
                    return actions
                }
            }
        }
        // const actions = this.TaskMap[memory.tasktype]?.getActions(arg);
        // if(actions.length>0)
        // {
        //     return actions
        // }
        return [ ];
    }
}
